{ include("techAnalisysInit.asl") }

// Agent technicalAnalyst in project StockMarket

/* Initial beliefs and rules */
performance :- money(X) & X > 250000.
confidence(S,D2,P1) :- buyBias[doc(_,S,D1,P2)] &  D2 >= D1 & (P2 < P1 | not portfolio(S,_,_)).
evaluateBuy(S,P1) :- portfolio(S,P2,_) &  (P2 <= 1.005*P1 | P1 < P2).
evaluateSell(S,P1) :- portfolio(S,P2,_) &  (P2 >= 1.005*P1 | P1 > P2).


/* Initial goals */
!discover_art("Money").


+agressivity(Money, Agressivity) : performance(Money)
 <- .random(Agressivity);
 	Agressivity = Agressivity+Money/1000;
 .
+agressivity(Money, Agressivity)
 <- .random(Agressivity);
 	Agressivity = Agressivity-Money/1000;
.
/* Plans */
+money(X)
<-	.print("Money -------------> ",X);
.
+!tryToSell(Stock, V, Price) : tryK & portfolio(Stock,_,_) 
<-	!findBest("K", Stock, Max);
	!sell(Stock, V, Price);
.

+!tryToSell(Stock, V, Price) : portfolio(Stock,_,_) 
<-	!findBest("K", Stock, Max);
	!sell(Stock, V, Price);
.


+!tryToSell(Stock, OBV, Price)
<- .print("Nothing to sell");
.
-!tryToSell(Stock, OBV, Price) [error(E),error_msg(Msg),code(Cmd),code_src(Src),code_line(Line)]
   <- .print("Failed to exec  due to: ",Msg," (",E,"). Command: ",Cmd, " on ",Src,":", Line)
.
+!sell(Stock, V, Price) : evaluateSell(Stock, Price)
<- !dealSell(Stock, Price);
.

+!sell(Stock, V, Price)
<- .print("Nothing to sell");
.

+!tryToBuy(Stock, OBV, Price) : tryOBV 
<-	!findBest("OBV", Stock, Max);
	!buy(Stock, OBV, Price, Max);
	.print(Stock," OBV MAX------>", Max);
.

+!tryToBuy(Stock, K, Price) : tryK & K 
<-	!findBest("K", Stock, Max);
	!buy(Stock, K, Price, Max);
	.print(Stock," K MAX------>", Max);
.


//wait for the believes to be up to date
+!tryToBuy(Stock, OBV, Price)
<- 	.wait(100);
	!!tryToBuy(Stock, OBV, Price);
.


+!findBest(V, S, M)
<-	.findall(X, buyBias[doc(V,S,X,_)], L);
	.print("BIAS ", L);
	.max(L,M);
.

-!findBest (V,S,M)[error(E),error_msg(Msg),code(Cmd),code_src(Src),code_line(Line)]
   <- .print("Failed to exec  due to: ",Msg," (",E,"). Command: ",Cmd, " on ",Src,":", Line)
.

+!dealBuy(Stock, Price)
<- 	!discover_art("Money");
	buy(Price, 1000, Stock);
	!calculatePortfolio(Stock, Price, 1000);
	?money(X);
	.print("BUY  ++++++++++> ", Stock, "++++++++++> ",Price, "-------->", X);
.

@lin[atomic]
+!calculatePortfolio(Stock, Price, V)
<-	?portfolio(Stock, Price, Q);
	Q1 = Q + V;
	-portfolio(Stock, Price, Q);
	+portfolio(Stock, Price, Q1);
	.print(Stock, "    ", Price,"   ", Q1, "   ",Q);
.
-!calculatePortfolio(Stock, Price, V)
<- 	if (V > 0)
	{
		+portfolio(Stock, Price, 1000);
	}
	else
	{
		-portfolio(Stock, Price, 1000);
	}
.

+!dealSell(Stock, Price)
<- 	!discover_art("Money");
	.findall(X, portfolio(Stock, X,_), L);
	.min(L, P);
	sell(Price, 1000, Stock);
	!calculatePortfolio(Stock, P, -1000);
	?money(X);
	.print("SELL ++++++++++> ", Stock, "++++++++++> ",Price, "-------->", X);
.

-!deal [error(E),error_msg(Msg),code(Cmd),code_src(Src),code_line(Line)]
   <- .print("Failed to exec  due to: ",Msg," (",E,"). Command: ",Cmd, " on ",Src,":", Line)
.

+!buy(Stock, V, Price, Max) : stockOpen & tryOBV &
			 				  (confidence(Stock, Max, Price) | evaluateBuy(Stock, Price)) &
			 				  performance
<-	!dealBuy(Stock, Price);
	-tryOBV;
	.random(X);
	+buyByOBV(Stock,X);
//	-+agressivity(X,Y);
.

+!buy(Stock, V, Price, Max) : stockOpen & tryK &
			 				  confidence(Stock, Max, Price)  &
			 				  performance
<-	!dealBuy(Stock, Price);
	-tryK;
	.random(X);
	+buyByK(Stock,X);
//	-+agressivity(X,Y);
.
//wait for the believes to be up to date
+!buy(Stock, V, Price, Max)
<- .print("Nothing to buy")
.

-!buy(Stock, V, Price, Max)[error(E),error_msg(Msg),code(Cmd),code_src(Src),code_line(Line)]
   <- .print("Failed to exec  due to: ",Msg," (",E,"). Command: ",Cmd, " on ",Src,":", Line)
.